<?php
require_once 'Validate.php';
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
/**
 * Validator
 * 
 * @package 
 * @version $id$
 * @author kamisiro.memo <kamisiro.memo@gmail.com> 
 */
class Validator extends Validate
{
    /**
     * [validates] 
     *
     * 値検証用の配列
     * 
     * @var array
     * @access public
     */
    var $validates = array();

    /**
     * [error_msg] 
     * 
     * @var array
     * @access public
     */
    var $error_msg = array();

    /**
     * [error_flg] 
     * 
     * @var mixed
     * @access public
     */
    var $error_flg = false;

    /**
     * [error_field] 
     * 
     * @var array
     * @access public
     */
    var $error_field = array();

    /**
     * [validate] 
     *
     * 値検証を行うメソッド
     * 
     * @param mixed $req 
     * @access public
     * @return void
     */
    public function validate($req)
    {
        $validates = $this->validates;    
        $error_msg = array();
        $error_flg = true;

        foreach ($validates as $field => $valid) {
            
            //そもそもフィールドが存在しない場合はチェックを行わない
            if (isset($req[$field]) === false) {
                continue; 
            }
            
            foreach ($valid as $rule => $detail) {

                if ($detail['empty'] === true && $req[$field] == '') {
                    continue;
                }
                
                $result = true;
                if (method_exists($this, $detail['rule']) === false) {
                    trigger_error('バリデートメソッド:' . $detail['rule'] . 'が存在しません');
                    continue;
                }

                $result = call_user_func(array($this, $detail['rule']), $req[$field]);

                if ($result === false) {
                    $error_flg   = false;
                    $error_msg[] = $detail['msg'];
                    $error_field[$field] = "error";
                }

            }
        }
        
        $this->error_msg    = $error_msg;
        $this->error_flg    = $error_flg;
        $this->error_field  = $error_field;

        return $error_flg;
    }
}

